package service;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;
import Dao.QueryMsgDao;
import bean.LoginResult;
import bean.QueryMessageResult;

/**
 * Servlet implementation class GetMessageService
 */
public class GetMessageService extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public GetMessageService() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		HttpSession session=request.getSession();
		LoginResult loginResult=new LoginResult();
		QueryMsgDao dao=new QueryMsgDao();
		QueryMessageResult queryMessageResult=new QueryMessageResult();
		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		PrintWriter writer = response.getWriter();
		if(session.getAttribute("loginResult")==null)
		{
			queryMessageResult.setStatus(-1);
		}else{
		loginResult=(LoginResult) session.getAttribute("loginResult");
		int userid=loginResult.getUserid();
		queryMessageResult=dao.getMessage(userid);
		}
		JSONObject json=JSONObject.fromObject(queryMessageResult);
		writer.println(json);
		writer.flush();
		writer.close();
		
	}

}
